package com.wnhz.work.jy.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.MyApplication;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2016/7/21 0021.
 * 密码输入框
 */
public class InputPasswordActivity extends BaseActivity implements OnClickListener {
    private TextView title;
    private EditText one_ed, two_ed, old_ed;
    private List<String> old_List = new ArrayList<>();
    private List<String> one_List = new ArrayList<>();
    private List<String> two_List = new ArrayList<>();
    private TextView old_TextView1, old_TextView2, old_TextView3,
            old_TextView4, old_TextView5, old_TextView6;
    private TextView one_TextView1, one_TextView2, one_TextView3,
            one_TextView4, one_TextView5, one_TextView6;
    private TextView two_TextView1, two_TextView2, two_TextView3,
            two_TextView4, two_TextView5, two_TextView6;
    private View lay_one, lay_two;
    private List<TextView> old_tv_list = new ArrayList<>();
    private List<TextView> one_tv_list = new ArrayList<>();
    private List<TextView> two_tv_list = new ArrayList<>();

    private String type;

    private View old_lay;

    private View one_lay, two_lay;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_inputpassword);
        type = getIntent().getStringExtra("type");

        initView();
        initTirle();
    }

    private void initTirle() {
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("设置支付密码");
        old_lay.setVisibility(View.GONE);
        /*if ("重置支付密码".equals(type)){
            title.setText("重置支付密码");
			if (old_lay.getVisibility() == View.GONE){
				old_lay.setVisibility(View.VISIBLE);
			}
		}else {
			title.setText("设置支付密码");
			if (old_lay.getVisibility() == View.VISIBLE){
				old_lay.setVisibility(View.GONE);
			}
		}*/

    }

    private void initView() {
        old_lay = findViewById(R.id.old_lay);
        lay_one = findViewById(R.id.lay_one);
        //lay_one.setOnClickListener(this);
        lay_two = findViewById(R.id.lay_two);
        //lay_two.setOnClickListener(this);

        one_lay = lay_one.findViewById(R.id.one_lay);
        two_lay = lay_two.findViewById(R.id.one_lay);

        findViewById(R.id.tv_submit).setOnClickListener(this);

        initOldTextview();
        initOneTextview();
        initTwoTextview();
        old_ed = (EditText) findViewById(R.id.old_ed);
        old_ed.addTextChangedListener(oldTextWatcher);
        one_ed = (EditText) findViewById(R.id.one_ed);
        one_ed.addTextChangedListener(oneTextWatcher);
        //one_ed.setOnEditorActionListener(onelistener);
        two_ed = (EditText) findViewById(R.id.two_ed);
        two_ed.addTextChangedListener(twoTextWatcher);
        //two_ed.setOnEditorActionListener(twolistener);
        one_ed.setOnFocusChangeListener(one_fo_listener);
        two_ed.setOnFocusChangeListener(two_fo_listener);

        one_ed.setOnClickListener(this);
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            public void run() {
                InputMethodManager inputManager = (InputMethodManager) one_ed
                        .getContext().getSystemService(
                                Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(one_ed, 0);
            }

        }, 198);
    }

    View.OnFocusChangeListener one_fo_listener = new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            one_lay.setSelected(hasFocus);
        }
    };
    View.OnFocusChangeListener two_fo_listener = new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            two_lay.setSelected(hasFocus);
        }
    };
    OnEditorActionListener onelistener = new OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId,
                                      KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {

            }
            return true;
        }
    };
    TextWatcher oldTextWatcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
            // TODO Auto-generated method stub
            System.out.println("onTextChanged" + "|CharSequence" + s + "|start"
                    + start + "|count" + count + "|before" + before);
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            System.out.println("beforeTextChanged" + "|CharSequence" + s
                    + "|start" + start + "|count" + count);

        }

        @Override
        public void afterTextChanged(Editable s) {
            System.out.println("afterTextChanged" + s);

            old_List.clear();
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                old_List.add(c + "");
            }
            setonetextview(old_List, old_tv_list);
        }

    };
    TextWatcher oneTextWatcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
            // TODO Auto-generated method stub
            System.out.println("onTextChanged" + "|CharSequence" + s + "|start"
                    + start + "|count" + count + "|before" + before);
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            System.out.println("beforeTextChanged" + "|CharSequence" + s
                    + "|start" + start + "|count" + count);

        }

        @Override
        public void afterTextChanged(Editable s) {
            System.out.println("afterTextChanged" + s);

            one_List.clear();
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                one_List.add(c + "");
            }
            setonetextview(one_List, one_tv_list);
        }

    };

    OnEditorActionListener twolistener = new OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId,
                                      KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {

            }
            return true;
        }
    };


    TextWatcher twoTextWatcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
            // TODO Auto-generated method stub
            System.out.println("onTextChanged" + "|CharSequence" + s + "|start"
                    + start + "|count" + count + "|before" + before);
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            System.out.println("beforeTextChanged" + "|CharSequence" + s
                    + "|start" + start + "|count" + count);

        }

        @Override
        public void afterTextChanged(Editable s) {
            System.out.println("afterTextChanged" + s);
            two_List.clear();
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                two_List.add(c + "");
            }
            setonetextview(two_List, two_tv_list);

        }

    };

    private void setonetextview(List<String> List, List<TextView> vList) {
        if (List.size() == 0) {
            for (int i = 0; i < vList.size(); i++) {
                vList.get(i).setText("");
            }
        } else if (List.size() == 1) {
            for (int i = 0; i < vList.size(); i++) {
                if (i < 1) {
                    vList.get(i).setText(List.get(i));
                } else {
                    vList.get(i).setText("");
                }
            }
        } else if (List.size() == 2) {
            for (int i = 0; i < vList.size(); i++) {
                if (i < 2) {
                    vList.get(i).setText(List.get(i));
                } else {
                    vList.get(i).setText("");
                }
            }
        } else if (List.size() == 3) {
            for (int i = 0; i < vList.size(); i++) {
                if (i < 3) {
                    vList.get(i).setText(List.get(i));
                } else {
                    vList.get(i).setText("");
                }
            }
        } else if (List.size() == 4) {
            for (int i = 0; i < vList.size(); i++) {
                if (i < 4) {
                    vList.get(i).setText(List.get(i));
                } else {
                    vList.get(i).setText("");
                }
            }
        } else if (List.size() == 5) {
            for (int i = 0; i < vList.size(); i++) {
                if (i < 5) {
                    vList.get(i).setText(List.get(i));
                } else {
                    vList.get(i).setText("");
                }
            }
        } else if (List.size() == 6) {
            for (int i = 0; i < vList.size(); i++) {
                if (i < 6) {
                    vList.get(i).setText(List.get(i));
                } else {
                    vList.get(i).setText("");
                }
            }
        }
    }

    private void initOldTextview() {
        old_TextView1 = (TextView) old_lay.findViewById(R.id.one_pay_passwork1);
        old_TextView2 = (TextView) old_lay.findViewById(R.id.one_pay_passwork2);
        old_TextView3 = (TextView) old_lay.findViewById(R.id.one_pay_passwork3);
        old_TextView4 = (TextView) old_lay.findViewById(R.id.one_pay_passwork4);
        old_TextView5 = (TextView) old_lay.findViewById(R.id.one_pay_passwork5);
        old_TextView6 = (TextView) old_lay.findViewById(R.id.one_pay_passwork6);
        old_tv_list.add(old_TextView1);
        old_tv_list.add(old_TextView2);
        old_tv_list.add(old_TextView3);
        old_tv_list.add(old_TextView4);
        old_tv_list.add(old_TextView5);
        old_tv_list.add(old_TextView6);
    }

    private void initOneTextview() {
        one_TextView1 = (TextView) lay_one.findViewById(R.id.one_pay_passwork1);
        one_TextView2 = (TextView) lay_one.findViewById(R.id.one_pay_passwork2);
        one_TextView3 = (TextView) lay_one.findViewById(R.id.one_pay_passwork3);
        one_TextView4 = (TextView) lay_one.findViewById(R.id.one_pay_passwork4);
        one_TextView5 = (TextView) lay_one.findViewById(R.id.one_pay_passwork5);
        one_TextView6 = (TextView) lay_one.findViewById(R.id.one_pay_passwork6);
        one_tv_list.add(one_TextView1);
        one_tv_list.add(one_TextView2);
        one_tv_list.add(one_TextView3);
        one_tv_list.add(one_TextView4);
        one_tv_list.add(one_TextView5);
        one_tv_list.add(one_TextView6);
    }

    private void initTwoTextview() {
        two_TextView1 = (TextView) lay_two.findViewById(R.id.one_pay_passwork1);
        two_TextView2 = (TextView) lay_two.findViewById(R.id.one_pay_passwork2);
        two_TextView3 = (TextView) lay_two.findViewById(R.id.one_pay_passwork3);
        two_TextView4 = (TextView) lay_two.findViewById(R.id.one_pay_passwork4);
        two_TextView5 = (TextView) lay_two.findViewById(R.id.one_pay_passwork5);
        two_TextView6 = (TextView) lay_two.findViewById(R.id.one_pay_passwork6);
        two_tv_list.add(two_TextView1);
        two_tv_list.add(two_TextView2);
        two_tv_list.add(two_TextView3);
        two_tv_list.add(two_TextView4);
        two_tv_list.add(two_TextView5);
        two_tv_list.add(two_TextView6);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lay_one:
                setoneed();
                break;

            case R.id.lay_two:
                settwoed();
                break;

            case R.id.btn_main_left:
                finish();
                break;

            //提交
            case R.id.tv_submit:
                submit();
                break;

            default:
                break;
        }

    }

    private boolean isTrue;

    private static final String TAG = "InputPasswordActivity";

    private void submit() {
        if (one_ed.getText().toString().length() == 6 && two_ed.getText().toString().length() == 6) {
            if (one_ed.getText().toString().equals(two_ed.getText().toString())) {
                Map<String, Object> map = new HashMap<>();
                map.put("userId", MyApplication.getInstance().userBean.getUserId());
                map.put("payPassword", two_ed.getText().toString());
                showDialog();
                XUtil.Post(ConfigUtil.MODIFY_PAYPASS, map, new MyCallBack<String>() {
                    @Override
                    public void onSuccess(String result) {
                        super.onSuccess(result);
                        Log.i(TAG, "onSuccess: Password" + result);
                        try {
                            JSONObject jsonObject = new JSONObject(result);
                            String res = jsonObject.optString("result");
                            String info = jsonObject.optString("info");

                            if ("1".equals(res)) {
                                Toast.makeText(InputPasswordActivity.this, info, Toast.LENGTH_SHORT).show();
                                /*Intent intent = new Intent(InputPasswordActivity.this, AdminAccountActivity.class);
                                startActivity(intent);*/
                                MyApplication.getInstance().userBean.setPayPassword("1");
                                finish();
                            } else if ("2".equals(res)) {
                                Toast.makeText(InputPasswordActivity.this, info, Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        super.onError(ex, isOnCallback);
                    }

                    @Override
                    public void onFinished() {
                        super.onFinished();
                        closeDialog();
                    }
                });
            } else {
                Toast.makeText(InputPasswordActivity.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(InputPasswordActivity.this, "请填写完整密码", Toast.LENGTH_SHORT).show();
        }


    }

    private void setoneed() {
        guanb();
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            public void run() {
                InputMethodManager inputManager = (InputMethodManager) one_ed
                        .getContext().getSystemService(
                                Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(one_ed, 0);
            }

        }, 0);

    }

    private void settwoed() {
        guanb();
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            public void run() {
                InputMethodManager inputManager = (InputMethodManager) two_ed
                        .getContext().getSystemService(
                                Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(two_ed, 0);
            }

        }, 0);

    }

    private void guanb() {
        View view = getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputmanger = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }


}